package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.entity.Course;
import com.zhentao.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/api/course")
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    
    /**
     * 获取课程详情
     * 
     * @param id 课程ID
     * @return 课程详情
     */
    @GetMapping("/detail/{id}")
    public Result<Course> getCourseDetail(@PathVariable Integer id) {
        try {
            Course course = courseService.getCourseById(id);
            if (course == null) {
                return Result.error("课程不存在");
            }
            return Result.success(course);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取课程详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取课程列表
     * 
     * @return 课程列表
     */
    @GetMapping("/list")
    public Result<List<Course>> getCourseList() {
        try {
            List<Course> courseList = courseService.getAllCourses();
            return Result.success(courseList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取课程列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取推荐课程列表
     * 
     * @return 推荐课程列表
     */
    @GetMapping("/recommended")
    public Result<List<Course>> getRecommendedCourses() {
        try {
            List<Course> courseList = courseService.getRecommendedCourses();
            return Result.success(courseList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取推荐课程失败：" + e.getMessage());
        }
    }
    
    /**
     * 购买课程
     * 
     * @param courseId 课程ID
     * @return 结果
     */
    @PostMapping("/purchase/{courseId}")
    public Result<String> purchaseCourse(@PathVariable Integer courseId) {
        try {
            // TODO: 实现购买逻辑
            Course course = courseService.getCourseById(courseId);
            if (course == null) {
                return Result.error("课程不存在");
            }
            return Result.success("购买成功", "购买成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("购买失败：" + e.getMessage());
        }
    }
}

